/*
 * Authors: Alex Attar & Kevin El Hage
 * ID: 9765727
 * Date: 9/3/2012
 * 
 */

package taskManagerSystem.ModelSubsystem;

import java.io.*;
import java.util.Vector;

import javax.xml.parsers.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * The IOHandler class contains static methods used to save and load files.
 */
public class IOHandler {
	/*
	 * loadTasksFile reads information regarding tasks from a .xml file and 
	 * returns a Vector of tasks. 
	 */
	public static Vector<Task> loadTasksFile(String filename){
		Vector<Task> tasks = new Vector<Task>();
		
		File file = new File(filename);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList taskLst = doc.getElementsByTagName("Task");
			for(int i=0; i!=taskLst.getLength(); i++){
				Task task; 
				Node taskNode = taskLst.item(i);
				Element taskElem = (Element) taskNode;
				
				Element taskIDE      = (Element) taskElem.getElementsByTagName("TaskID").item(0);
				Element titleE       = (Element) taskElem.getElementsByTagName("Title").item(0);
				Element descriptionE = (Element) taskElem.getElementsByTagName("Description").item(0);
				Element durationE    = (Element) taskElem.getElementsByTagName("Duration").item(0);
				Element deliverableE = (Element) taskElem.getElementsByTagName("Deliverable").item(0);
				Element deadlineE    = (Element) taskElem.getElementsByTagName("DeadlineDate").item(0);
				Element personE      = (Element) taskElem.getElementsByTagName("Person").item(0);
				Element parentTaskIDE= (Element) taskElem.getElementsByTagName("ParentTaskID").item(0);
				Element prerequisiteTaskIDE = (Element) taskElem.getElementsByTagName("PrerequisiteTask").item(0);

				
				String taskID 		= taskIDE.getChildNodes().item(0).getNodeValue();
				String title 		= titleE.getChildNodes().item(0).getNodeValue();
				String description	= descriptionE.getChildNodes().item(0).getNodeValue();
				String duration		= durationE.getChildNodes().item(0).getNodeValue();
				String deliverable 	= deliverableE.getChildNodes().item(0).getNodeValue();
				String deadline 	= deadlineE.getChildNodes().item(0).getNodeValue();
				String person 		= personE.getChildNodes().item(0).getNodeValue();
				int parentTaskID    = Integer.parseInt(parentTaskIDE.getChildNodes().item(0).getNodeValue());
				int prerequisiteTaskID    = Integer.parseInt(prerequisiteTaskIDE.getChildNodes().item(0).getNodeValue());

				task = new Task(Integer.parseInt(taskID), title, description, Integer.parseInt(duration),
						deliverable, deadline, person, parentTaskID, prerequisiteTaskID);
				tasks.add(task);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return tasks;
	}
	
	/*
	 * saveTaskFile iterates through a Vector of tasks and outputs each one
	 * as a properly formatted XML element to the chosen file.
	 */
	public static void saveTaskFile(Vector<Task> taskList, String filename){
		PrintWriter outputStream = null;
		try
		{
			outputStream = new PrintWriter(new FileOutputStream(filename));
		}
		catch(FileNotFoundException e)
		{
			System.exit(0);
		}
		
		outputStream.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		outputStream.println("<Tasks>");

		for (int i = 0; i < taskList.size(); i++)
		{
			Task task = taskList.get(i);
			outputStream.println("\t<Task>");
			outputStream.println("\t\t<TaskID>" + task.getTaskID() + "</TaskID>");
			outputStream.println("\t\t<Title>" + task.getTitle() + "</Title>");
			outputStream.println("\t\t<Description>" + task.getDescription() + "</Description>");
			outputStream.println("\t\t<Duration>" + task.getDuration() + "</Duration>");
			outputStream.println("\t\t<Deliverable>" + task.getDeliverable() + "</Deliverable>");
			outputStream.println("\t\t<DeadlineDate>" + task.getDeadlineDate() + "</DeadlineDate>");
			outputStream.println("\t\t<Person>" + task.getPerson() + "</Person>");
			outputStream.println("\t\t<ParentTaskID>" + task.getParentTaskId() + "</ParentTaskID>");
			outputStream.println("\t\t<PrerequisiteTask>" + task.getParentTaskId() + "</PrerequisiteTask>");
			
			outputStream.println("\t</Task>");
		}   
		
		outputStream.println("</Tasks>");
		outputStream.close();
	}
	
	/*
	 * savePersonFile iterates through a Vector of people and simply outputs each one
	 * to a line in the file using the Person toString() method.
	 */
	public static void savePersonFile(Vector<Person> personList, String filename){
		PrintWriter outputStream = null;
		try
		{
			outputStream = new PrintWriter(new FileOutputStream(filename));
		}
		catch(FileNotFoundException e)
		{
			System.exit(0);
		}

		for (int i = 0; i < personList.size(); i++)
		{
			outputStream.println(personList.get(i).toString());
		}   

		outputStream.close();
	}
}
